#pragma once
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <unistd.h>
#include <fcntl.h>
#include <cstdlib>
#include <string>
#include <fstream>
#include <sstream>
#include<map>

namespace Ns_FileTools
{
    using namespace std;
    class FileTools //方便文件读写的工具类
    {
    public:
        static bool FileRedirection(const std::string& filename,int fd)
        {
            int new_fd = open(filename.c_str(), O_RDWR | O_CREAT | O_APPEND, 0644);
            if (new_fd == -1)
            {
                return false;
            }
            if (dup2(new_fd, fd) == -1)
            {
                close(new_fd);
                return false;
            }
            close(new_fd);
            return true;
        }
        static bool WriteStringToFile(const string& content,const string& filepath)
        {
            // 打开文件流，使用 std::ios::app 模式以追加内容
            std::ofstream file(filepath, std::ios::app);
            // 检查文件是否成功打开
            if (!file.is_open()) {
                return false;
            }
            // 将内容写入文件
            file << content;
            // 关闭文件流
            file.close();
            return true;
        }
        static bool ReadFileToString(const string& filepath, string& out_string)
        {
            ifstream ifs(filepath, ifstream::in);
            if (!ifs)
            {
                return false;
            }
            stringstream ss;
            ss << ifs.rdbuf();
            out_string = ss.str();
        
            return true;
        }
        //创建文件并写入
        static bool CreateFileAndWrite(const std::string& filename,const std::string& content)
        {
            // 创建并打开文件
            std::ofstream file(filename, std::ios_base::out | std::ios_base::trunc);
    
            if (!file.is_open()) {
                return false;
            }

            // 写入内容
            file << content;

            // 检查写入是否成功
            if (file.fail()) {
                return false;
            }

            // 关闭文件
            file.close();

            return true;
        }
        static string GenerateUniqueFileName(const std::string& extension = "")
        {
            // 生成 UUID
            boost::uuids::uuid uuid = boost::uuids::random_generator()();
    
            // 将 UUID 转换为字符串
            std::string uuid_str = boost::uuids::to_string(uuid);
        
            // 如果有扩展名，添加扩展名
            if (!extension.empty()) {
                uuid_str += "." + extension;
            }
    
            return uuid_str;
        }
        static void FilesClean(const std::string& filename)
        {
            //清理
            if (access((filename+".cpp").c_str(), F_OK) == 0)
            {
                unlink((filename+".cpp").c_str());
            }
            if (access(filename.c_str(), F_OK) == 0)
            {
                unlink(filename.c_str());
            }
            if (access((filename+".stdin").c_str(), F_OK) == 0)
            {
                unlink((filename+".stdin").c_str());
            }
            if (access((filename+".stdout").c_str(), F_OK) == 0)
            {
                unlink((filename+".stdout").c_str());
            }
            if (access((filename+".stderr").c_str(), F_OK) == 0)
            {
                unlink((filename+".stderr").c_str());
            }
        }
        // 移除字符串首尾的空白字符
        static string trim(const std::string& str) {
            size_t first = str.find_first_not_of(' ');
            if (std::string::npos == first) {
                return str;
            }
            size_t last = str.find_last_not_of(' ');
            return str.substr(first, (last - first + 1));
        }
        static map<std::string, std::string> parse_cookie(const std::string& cookie_header)
        {
            std::map<std::string, std::string> cookie_map;
            std::string cookie = cookie_header;

            // 移除"Cookie: "前缀
            size_t colon_pos = cookie.find(": ");
            if (colon_pos != std::string::npos) {
                cookie = cookie.substr(colon_pos + 2);
            }

            // 解析cookie键值对
            size_t start = 0, end;
            while ((end = cookie.find(';', start)) != std::string::npos) {
                std::string pair = cookie.substr(start, end - start);
                size_t equals_pos = pair.find('=');
                if (equals_pos != std::string::npos) {
                    std::string key = pair.substr(0, equals_pos);
                    std::string value = pair.substr(equals_pos + 1);
                    key=trim(key);
                    value=trim(value);
                    cookie_map[key] = value;
                }
                start = end + 1;
            }

            // 解析最后一个键值对
            if (start < cookie.length()) {
                std::string pair = cookie.substr(start);
                size_t equals_pos = pair.find('=');
                if (equals_pos != std::string::npos) {
                    std::string key = pair.substr(0, equals_pos);
                    std::string value = pair.substr(equals_pos + 1);
                    key=trim(key);
                    value=trim(value);
                    cookie_map[key] = value;
                }
            }

            return cookie_map;
        }
    };
}